package com.dieusoft.box.shoufatong.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dieusoft.box.shoufatong.entity.ShipContactsInfo;
import com.dieusoft.box.shoufatong.service.IShipContactsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 海运收发通信息
 * @Author: jeecg-boot
 * @Date: 2021-05-09
 * @Version: V1.0
 */
@Api(tags = "海运收发通信息")
@RestController
@RequestMapping("/box.shoufatong/shipContactsInfo")
@Slf4j
public class ShipContactsInfoController extends JeecgController<ShipContactsInfo, IShipContactsInfoService> {
    @Autowired
    private IShipContactsInfoService shipContactsInfoService;

    /**
     * 分页列表查询
     *
     * @param shipContactsInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ShipContactsInfo shipContactsInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<ShipContactsInfo> queryWrapper = QueryGenerator.initQueryWrapper(shipContactsInfo, req.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        queryWrapper.eq("sys_org_code", sysUser.getOrgCode());
        Page<ShipContactsInfo> page = new Page<ShipContactsInfo>(pageNo, pageSize);
        IPage<ShipContactsInfo> pageList = shipContactsInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 获取全部数据
     *
     * @return
     */
    @GetMapping(value = "/getall")
    @Cacheable(value = "shipContactsInfo")
    public Result getAll() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<ShipContactsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sys_org_code", sysUser.getOrgCode());
        List<ShipContactsInfo> list = shipContactsInfoService.list(queryWrapper);
        return Result.OK(list);
    }

    /**
     * 添加
     *
     * @param shipContactsInfo
     * @return
     */
    @PostMapping(value = "/add")
    @CacheEvict(value = "shipContactsInfo", allEntries = true)
    public Result<?> add(@RequestBody ShipContactsInfo shipContactsInfo) {
        shipContactsInfoService.save(shipContactsInfo);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param shipContactsInfo
     * @return
     */
    @PutMapping(value = "/edit")
    @CacheEvict(value = "shipContactsInfo", allEntries = true)
    public Result<?> edit(@RequestBody ShipContactsInfo shipContactsInfo) {
        shipContactsInfoService.updateById(shipContactsInfo);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @CacheEvict(value = "shipContactsInfo", allEntries = true)
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        shipContactsInfoService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.shipContactsInfoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "海运收发通信息-通过id查询")
    @ApiOperation(value = "海运收发通信息-通过id查询", notes = "海运收发通信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        ShipContactsInfo shipContactsInfo = shipContactsInfoService.getById(id);
        if (shipContactsInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(shipContactsInfo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param shipContactsInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ShipContactsInfo shipContactsInfo) {
        return super.exportXls(request, shipContactsInfo, ShipContactsInfo.class, "海运收发通信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @CacheEvict(cacheNames = "shipContactsInfo", allEntries = true)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ShipContactsInfo.class);
    }

}
